﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HAVTKPack;

using HA.ModFlow.Domain.DomainLayer;

namespace HA.ModFlow.Application
{
    public  class AlgorithmHelper
    {
        /// <summary>
        /// 返回结构为Z,Y,X 起算点位于最顶层左下角
        /// </summary>
        /// <param name="point3D"></param>
        /// <returns></returns>
        public static DotSpatial.Topology.Coordinate[, ,] ConvertCoordinatesFor2D(Point3D[, ,] point3D, double zMagnification)
        {
            //point3D存储结构为iY,jX,kZ
            DotSpatial.Topology.Coordinate[, ,] coords = new DotSpatial.Topology.Coordinate[point3D.GetLength(2), point3D.GetLength(0), point3D.GetLength(1)];
            Point3D pt;
            for (int k = 0; k < point3D.GetLength(2); k++)
            {
                for (int i = 0; i < point3D.GetLength(0); i++)
                {
                    for (int j = 0; j < point3D.GetLength(1); j++)
                    {

                        pt = point3D[i, j, k];

                        coords[k, i, j] = new DotSpatial.Topology.Coordinate(pt.X, pt.Y, pt.Z*zMagnification);

                    }

                }

            }

            return coords;
        }

        public static HydrolicGrid ConvertToHydroGrid(BoundGrid bg)
        {

            HydrolicGrid hg = new HydrolicGrid(bg.RowsCount, bg.ColumnsCount, bg.LayersCount);//RowsCount外围边框点数

            Point3D pt;

            for (int k = 0; k < bg.LayersCount; k++)
            {
                for (int i = 0; i < bg.RowsCount; i++)
                {
                    for (int j = 0; j < bg.ColumnsCount; j++)
                    {
                        pt = bg.GridPoints[i, j, k];

                        hg.Coords[i, j, k] = new Coordinate(pt.X,pt.Y,pt.Z);
                        
                    }
                    
                }
                
            }


            return hg;
 
        }

        public static HydrolicGrid ConvertToHydroGrid(HeadGrid bg)
        {

            HydrolicGrid hg = new HydrolicGrid(bg.RowsCount, bg.ColumnsCount, bg.LayersCount);//RowsCount外围边框点数

            Point3D pt;

            for (int k = 0; k < bg.LayersCount; k++)
            {
                for (int i = 0; i < bg.RowsCount; i++)
                {
                    for (int j = 0; j < bg.ColumnsCount; j++)
                    {
                        pt = bg.GridHeads [i, j, k];

                        hg.Coords[i, j, k] = new Coordinate(pt.X, pt.Y, pt.Z);


                    }

                }

            }


            return hg;

        }


        public static RectilinearGrid ConvertToRectilinearGrid(HeadGrid hg)
        {

            RectilinearGrid rg = new RectilinearGrid(hg.RowsCount, hg.ColumnsCount, hg.LayersCount);//RowsCount外围边框点数
            
            Point3D pt;

            for (int cols = 0; cols < hg.ColumnsCount; cols++)
            {
                 pt = hg.GridHeads[0 , cols, 0];

                 rg.XArray[cols] = (float)pt.X;

            }
            for (int rows = 0; rows < hg.RowsCount ; rows++)
            {
                pt=hg.GridHeads [rows ,0,0];

                rg.YArray[rows] = (float)pt.Y;
            }

            for (int layer = 0; layer < hg.LayersCount ; layer++)
            {
                pt=hg.GridHeads [0,0,layer ];

                rg.ZArray [layer] = (float)pt.Z;
            }

            return rg;

        }


        public static HydrolicLayer ConverToHydrolicLayer(Point3D[,] layerBound)
        {
            if (layerBound ==null )
            {
                return null;
            }
            HydrolicLayer hgLayer = new HydrolicLayer(layerBound .GetLength (0),layerBound .GetLength (1));//外围边框点数

            Point3D pt;

            for (int rows = 0; rows < layerBound .GetLength (0); rows++)
            {
                for (int cols  = 0; cols  < layerBound .GetLength (1); cols ++)
                {
                    pt =layerBound [rows ,cols ];
                    hgLayer .Coords [rows ,cols ]=new Coordinate (pt.X ,pt .Y ,pt .Z );

                }   
            }

            return hgLayer;

        }






    }
}
